package cn.gov.cus.rmftquartz.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 主数据源配置类。
 * @author wxming.
 * @date 2018/2/9.
 */
@Configuration
@MapperScan(basePackages = OracleDataSourceConfig.PACKAGE,sqlSessionTemplateRef = "oracleSqlSessionTemplate")
public class OracleDataSourceConfig {
    static final String PACKAGE ="cn.gov.cus.rmftquartz.mappers.oracle";

    @Value("${datasource.oracle.rmftras.url}")
    private String url;
    @Value("${datasource.oracle.rmftras.username}")
    private String user;
    @Value("${datasource.oracle.rmftras.password}")
    private String password;
    @Value("${datasource.oracle.rmftras.driver-class-name}")
    private String driverClassName;
    @Value("${datasource.oracle.rmftras.maxPoolSize}")
    private Integer maxPoolSize;

    public static Properties getDataSourceProperties(Environment env,String prefix){
        Properties prop = new Properties();
        String pre = "datasource.db.";
        prop.put("url",env.getProperty(prefix + "url"));
        prop.put("uesrname",env.getProperty(prefix + "uesrname"));
        prop.put("password",env.getProperty(prefix + "password"));
        prop.put("driverClassName",env.getProperty(prefix + "driver-class-name",""));
        prop.put("validationQuery",env.getProperty(prefix + "validationQuery"));
        prop.put("validationQueryTimeout",env.getProperty(prefix + "validationQueryTimeout",Integer.class));

        prop.put("initialSize",env.getProperty(pre + "initialSize",Integer.class));
        prop.put("maxActive",env.getProperty(pre + "maxActive",Integer.class));
        prop.put("maxWait",env.getProperty(pre + "maxWait",Integer.class));
        prop.put("timeBetweenEvictionRunsMillis",env.getProperty(pre + "timeBetweenEvictionRunsMillis",Integer.class));
        prop.put("minEvictableIdleTimeMillis",env.getProperty(pre + "minEvictableIdleTimeMillis",Integer.class));
        prop.put("testWhileIdle",env.getProperty(pre + "testWhileIdle",Boolean.class));
        prop.put("testOnBorrow",env.getProperty(pre + "testOnBorrow",Boolean.class));
        prop.put("testOnReturn",env.getProperty(pre + "testOnReturn",Boolean.class));

        prop.put("poolPreparedStatements",env.getProperty(pre + "poolPreparedStatements",Boolean.class));
        prop.put("maxPoolPreparedStatementPerConnectionSize",
                env.getProperty(pre + "maxPoolPreparedStatementPerConnectionSize",Integer.class));
        prop.put("filters",env.getProperty(pre + "filters"));
        prop.put("connectionProperties",env.getProperty(pre + "connectionProperties"));
        return prop;

    }

    @Bean(name = "oracleDataSource")
    @Autowired
    @Primary
    public DataSource oracleDataSource(Environment env){
        AtomikosDataSourceBean atomikosDataSource = new AtomikosDataSourceBean();
        Properties prop = getDataSourceProperties(env,"datasource.oracle.rmftras.");
        atomikosDataSource.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        atomikosDataSource.setUniqueResourceName("OracleDB");
        //因为大量多线程跑数据，限制了数据库的连接池最大为5,配置里配了5。
        atomikosDataSource.setMaxPoolSize(maxPoolSize);
        atomikosDataSource.setXaProperties(prop);
        return atomikosDataSource;
    }

    @Bean(name = "oracleSqlSessionFactory")
    @Primary
    public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception{
        final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();

        org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
        config.setMapUnderscoreToCamelCase(true);
        config.setCallSettersOnNulls(true);
        sessionFactoryBean.setConfiguration(config);
        sessionFactoryBean.setDataSource(oracleDataSource);
        return sessionFactoryBean.getObject();
    }

    @Bean(name = "oracleSqlSessionTemplate")
    public SqlSessionTemplate mydbSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory")SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
